#include "led.h"




static led_t led = 
{
    .ledPort[LED1] = GPIOA,
    .ledPort[LED2] = GPIOB,
    .ledPort[LED3] = GPIOB,
    .ledPort[LED4] = GPIOC,
    .ledPort[BUTTON_LED] = GPIOA,
    .ledPin[LED1] = GPIO_PIN_3,
    .ledPin[LED2] = GPIO_PIN_5,
    .ledPin[LED3] = GPIO_PIN_4,
    .ledPin[LED4] = GPIO_PIN_3,
    .ledPin[BUTTON_LED] = GPIO_PIN_1,
    .controlLed = ledOnOff,
    .status = LED_ONE
};
const led_t * const g_pLed = &led;//全局用
static led_t * const pLed = &led;//内部用

void ledInit()
{
    //LED输出IO配置
    //LED默认关闭灯
    for(int i = 0; i < LED_TOTAL; i++)
    {
        GPIO_Init(pLed->ledPort[i], pLed->ledPin[i], GPIO_MODE_OUT_PP_LOW_SLOW);
        pLed->controlLed(OFF, i);
    }
}

void updateLedStatus()
{
    if(pLed->status > LED_FOU)//需要闪烁的
    {
        //100ms
        static u8 flag = 0;//闪烁用
        f_onOff t_fOnOff;
        t_fOnOff = (flag ? ON : OFF);
        flag ^= 1;
        switch(pLed->status)
        {
            case LED_BUTTON:
                pLed->controlLed(t_fOnOff, BUTTON_LED);
                break;
            case LED_ONE_CHARGE:
                pLed->controlLed(t_fOnOff, LED1);
                break;
            case LED_TWO_CHARGE:
                pLed->controlLed(t_fOnOff, LED2);
                break;
            case LED_THR_CHARGE:
                pLed->controlLed(t_fOnOff, LED3);
                break;
            case LED_FOU_CHARGE:
                pLed->controlLed(t_fOnOff, LED4);
                break;
            default:break;
        }
    }
    else
    {
        switch(pLed->status)
        {
            case LED_ZER:
                pLed->controlLed(OFF, LED1);
                pLed->controlLed(OFF, LED2);
                pLed->controlLed(OFF, LED3);
                pLed->controlLed(OFF, LED4);
                break;
            case LED_ONE:
                pLed->controlLed(ON, LED1);
                pLed->controlLed(OFF, LED2);
                pLed->controlLed(OFF, LED3);
                pLed->controlLed(OFF, LED4);
                break;
            case LED_TWO:
                pLed->controlLed(ON, LED1);
                pLed->controlLed(ON, LED2);
                pLed->controlLed(OFF, LED3);
                pLed->controlLed(OFF, LED4);
                break;
            case LED_THR:
                pLed->controlLed(ON, LED1);
                pLed->controlLed(ON, LED2);
                pLed->controlLed(ON, LED3);
                pLed->controlLed(OFF, LED4);
                break;
            case LED_FOU:
                pLed->controlLed(ON, LED1);
                pLed->controlLed(ON, LED2);
                pLed->controlLed(ON, LED3);
                pLed->controlLed(ON, LED4);
                break;
            default:break;
        }
    }
      
}


void ledOnOff(f_onOff onOff, unsigned char ledNum)
{
    onOff(pLed->ledPort[ledNum], pLed->ledPin[ledNum]);
}

void setLedStatus(u8 status)
{
    pLed->status = status;         
}
